package com.scisoft.scan.ws;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scisoft.scan.data.QueryPageBean;
import com.scisoft.scan.data.delivery.DeliveryBean;
import com.scisoft.scan.data.delivery.DeliveryOrderBean;
import com.scisoft.scan.data.delivery.DeliveryQueryPageBean;
import com.scisoft.scan.data.depot.WarehouseBean;
import com.scisoft.scan.data.returngoods.ReturnGoodsDetailBean;
import com.scisoft.scan.data.returngoods.ReturnGoodsOrderBean;
import com.scisoft.scan.data.returngoods.ReturnGoodsReasonBean;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.HeaderProperty;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.List;


public class WebServiceHelper_ReturnGoods {

    public static  String GetWS_Url(){
        return String.format("http://%s/dymps/ws/ReturnGoodsServer?wsdl", SysParameter.WS_IPandPort);
    }

    public  static  String WS_Method = "receive";

    public  static  String WS_Namespace = "http://returnGoods.scan.ws.dms.com/";

    //查询退货清点单分页统计 (bizId 查询条件 flag 状态 // 1 未处理 2 已完成)
    public static QueryPageBean RETURNGOODS_01(String bizId,String flag)throws Exception {
//        QueryPageBean ret = new QueryPageBean();
//        ret.pageCount = 10;
//        ret.pageSize = 10;
//        ret.totalCount = 100;
//        return ret;
        if(flag==null || "".equals(flag)){
            flag="@@";
        }
        String biz = bizId + ","+ flag;
        String arg0 = String.format( "{\"flowNo\":\"RETURNGOODS_01\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,biz);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        QueryPageBean ret = new QueryPageBean();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<QueryPageBean>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }
    //查询退货清点单列表 (bizId 查询条件 flag 状态 // 1 未处理 2 已完成)
    public static List<ReturnGoodsOrderBean> RETURNGOODS_02(String bizId, String flag, int pagesize, int pageindex) throws Exception {
//        List<ReturnGoodsOrderBean> ret = new ArrayList<>();
//        ReturnGoodsOrderBean r = new ReturnGoodsOrderBean();
//        r.amount = "251.3";
//        r.depotName = "临时仓";
//        r.operator = "调配员";
//        r.optime = "20点15分";
//        r.orderInfo = "2项10件";
//        r.orderNo = "ST256256841256";
//        r.orderType = "产品升级退货";
//        r.customerName = "一汽大众汽车公司";
//        ret.add(r);
//        return ret;
        if(flag==null || "".equals(flag)){
            flag="@@";
        }
        String biz = bizId + ","+ flag;
        List<ReturnGoodsOrderBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"RETURNGOODS_02\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\",\"pageIndex\":\"%s\",\"pageSize\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,biz,pageindex,pagesize);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<ReturnGoodsOrderBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }
    //查询退货清点单明细
    public static List<ReturnGoodsDetailBean> RETURNGOODS_03(String orderId,String depotId,String depotAreaId) throws Exception {

//        List<ReturnGoodsDetailBean> ret = new ArrayList<>();
//        ReturnGoodsDetailBean bean = new ReturnGoodsDetailBean();
//        bean.dtlId = "1";
//        bean.partCode = "2";
//        bean.partName = "3";
//        bean.supplierCode = "4";
//        bean.supplierName = "5";
//        bean.imgUrl = "6";
//        bean.position = "7";
//        bean.partCount = "8";
//        bean.notReturnCount = "9";
//        bean.returnCount = "10";
//        bean.ifOk = "1";
//        bean.reason = "asd";
//        ret.add(bean);
//        ReturnGoodsDetailBean bean2 = new ReturnGoodsDetailBean();
//        bean2.dtlId = "2";
//        bean2.partCode = "2";
//        bean2.partName = "5";
//        bean2.supplierCode = "6";
//        bean2.supplierName = "7";
//        bean2.imgUrl = "8";
//        bean2.position = "9";
//        bean2.partCount = "10";
//        bean2.notReturnCount = "11";
//        bean2.returnCount = "12";
//        bean2.ifOk = "0";
//        ret.add(bean2);
//        return ret;
        String biz = orderId + ","+ depotId+","+depotAreaId;
        List<ReturnGoodsDetailBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"RETURNGOODS_03\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,biz);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<ReturnGoodsDetailBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }
    //配件退货数量提交
    public static String RETURNGOODS_04(String orderId,ReturnGoodsDetailBean returnGoodsDetailBean,String depotAreaId) throws Exception{
//        return "成功";
        Gson gson = new Gson();
        String parm =gson.toJson(returnGoodsDetailBean);
        String arg0 = String.format("{\"flowNo\":\"RETURNGOODS_04\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user,parm, SysParameter.ScSequence,depotAreaId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret="成功";
        if (msg!=null&&msg.contains("成功"))
        {

        }else
        {
            ret =msg;
            throw new Exception(msg);
        }
        return ret;
    }
    //订单完成提交
    public static String RETURNGOODS_05(String orderId)throws Exception {
//        return "成功";
        String arg0 = String.format("{\"flowNo\":\"RETURNGOODS_05\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence, orderId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {
        } else {
            throw new Exception(msg);
        }
        return msg;
    }
    //查询退货原因字典
    public static List<ReturnGoodsReasonBean> RETURNGOODS_06()throws Exception {
//        List<ReturnGoodsReasonBean> ret = new ArrayList<>();
//        ReturnGoodsReasonBean r1 = new ReturnGoodsReasonBean();
//        r1.ReasonCode = "1";
//        r1.ReasonName = "技术升级";
//        ret.add(r1);
//        ReturnGoodsReasonBean r2 = new ReturnGoodsReasonBean();
//        r2.ReasonCode = "2";
//        r2.ReasonName = "退货无效";
//        ret.add(r2);
//        return ret;
        String arg0 = String.format("{\"flowNo\":\"RETURNGOODS_06\",\"account\":\"%s\",\"scSequence\":\"%s\"}", SysParameter.user, SysParameter.ScSequence);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        List<ReturnGoodsReasonBean> gret = new ArrayList<ReturnGoodsReasonBean>();
        List<ReturnGoodsReasonBean> ret = new ArrayList<ReturnGoodsReasonBean>();
        if (msg != null && msg.contains("成功")) {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            gret = gson.fromJson(jsonArray.toString(), new TypeToken<List<ReturnGoodsReasonBean>>() {
            }.getType());
            ReturnGoodsReasonBean bean = new ReturnGoodsReasonBean("0", "");
            ret.add(bean);
            for (int i = 0; i < gret.size(); i++) {
                ReturnGoodsReasonBean tmp = gret.get(i);
                ret.add(tmp);
            }
        } else {
            throw new Exception(msg);
        }
        return ret;
    }


    public static  SoapObject CallWebservice(String arg0) throws Exception {
        //实例化SoapObject对象：命名空间+方法名称
        SoapObject request = new SoapObject(WS_Namespace, WS_Method);
        //设置WebService接口需要传入的参数
        request.addProperty("arg0", arg0);

        //创建SoapSerializationEnvelope 对象，同时指定soap版本号
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
        envelope.dotNet = false;//由于是.net开发的webservice，所以这里要设置为true

        //调用接口
        ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
        headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
        HttpTransportSE httpTransportSE = new HttpTransportSE(GetWS_Url());
        httpTransportSE.call(null, envelope,headerPropertyArrayList);//调用

        // 获取返回的数据
        SoapObject result = (SoapObject) envelope.bodyIn;
        return result;
    }
}
